<template>
	<basic-container>
		<avue-crud :option="option" :data="data" v-model="form" :page="page" ref="crud" @search-change="searchChange"
		 @search-reset="searchReset" @current-change="currentChange" @size-change="sizeChange" @row-del="rowDel" :before-open="beforeOpen"
		 @row-update="rowUpdate" @row-save="rowSave" @on-load="onLoad">
		</avue-crud>
	</basic-container>
</template>

<script>
	import request from '@/router/axios';

	export default {
		data() {
			return {
				dialogShow: false,
				form: {
					coordinate: {
						formattedAddress: "天安门广场",
						longitude: 116.39775500000002,
						latitude: 39.903179
					}
				},
				sublist: {},
				query: {},
				page: {
					pageSize: 10,
					currentPage: 1,
					total: 0
				},
				option: {
					dialogDrag: true,
					searchMenuSpan: 4,
					border: true,
					index: true,
					refreshBtn: false,
					excelBtn: true,
					menuWidth: '150px',
					column: [{
							label: "编号",
							prop: "code",
							width: "120px",
							disabled: true,
							placeholder: "系统自动生成"
						},
						{
							label: "名称",
							prop: "name",
							maxlength: 20,
							searchPlaceholder: '编号&名称',
							search: true,
							rules: [{
								required: true,
								trigger: "blur"
							}]
						},
						{
							label: "地址",
							maxlength: 50,
							prop: "address"
						},
						{
							label: "联系人",
							prop: "linkman",
							maxlength: 20,
							width: "120px"
						},
						{
							label: "联系方式",
							prop: "contact",
							maxlength: 20,
							width: "120px"
						},
						{
							label: '选择坐标',
							prop: 'coordinate',
							hide: true,
							component: 'AvueMap'
						}
					]
				},
				data: [],
			};
		},
		watch: {
			'form.coordinate'(val) {
				if (val != undefined && val != null) {
					this.form.formattedAddress = val.formattedAddress
					this.form.latitude = val.latitude;
					this.form.longitude = val.longitude;
				}
			}
		},
		computed: {},
		created() {},
		methods: {
			beforeOpen(done, type) {
				this.form.coordinate = {}
				if (["edit"].includes(type)) {
					request({
						url: '2/api/BaseOrganization/detail',
						params: {
							id: this.form.id
						}
					}).then(res => {
						const data = res.data.data;
						data.coordinate = {
							formattedAddress: data.formattedAddress,
							longitude: data.longitude,
							latitude: data.latitude
						};
						this.form = data;
					});
				} else {
					this.form = {};
					this.form.coordinate = {};
				}
				done();
			},
			rowSave(row, loading, done) {
				request({
					url: '2/api/BaseOrganization/add',
					method: 'post',
					data: row
				}).then(() => {
					loading();
					this.onLoad(this.query);
					done();
				}, error => {
					done();
					console.log(error);
				});
			},
			rowUpdate(row, index, loading, done) {
				return request({
					url: '2/api/BaseOrganization/edit',
					method: 'post',
					data: row
				}).then(() => {
					loading();
					this.onLoad(this.query);
				}, error => {
					done();
					console.log(error);
				});
			},
			rowDel(row) {
				this.$confirm("确定数据删除?", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					})
					.then(() => {
						return request({
							url: '2/api/BaseOrganization/remove',
							method: 'post',
							params: {
								ids: row.code
							}
						});
					})
					.then(() => {
						this.onLoad(this.page);
					});
			},
			searchReset() {
				this.query = {};
				this.page.currentPage = 1;
				this.onLoad(this.page);
			},
			searchChange(params, done) {
				this.page.currentPage = 1;
				this.query = params;
				this.onLoad(this.page);
				done();
			},
			currentChange(currentPage) {
				this.page.currentPage = currentPage;
			},
			sizeChange(pageSize) {
				this.page.pageSize = pageSize;
			},
			onLoad(page) {
				request({
					url: '2/api/BaseOrganization/getpages',
					params: {
						key: this.query.name == undefined ? '' : this.query.name,
						page: page.currentPage,
						limit: page.pageSize
					}
				}).then(res => {
					const data = res.data
					this.data = data.data;
					this.page.total = data.remark;
				});
			}
		}
	};
</script>

<style>
</style>
